package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 添加购物车纪录
func AddCart(c *gin.Context) {
	var code int
	var data model.Cart
	_ = c.ShouldBindJSON(&data)
	code = model.CreateCart(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// // 删除购物车纪录
func DeleteCarts(c *gin.Context) {
	var code int
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteGoods(id)
	if code == errmsg.ERROR_CARTS_NOT_EXIST {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}

// // 获取购物车列表
func GetCartList(c *gin.Context) {
	userId, _ := strconv.Atoi(c.Query("user_id"))
	data := model.GetCartList(userId)
	c.JSON(http.StatusOK, data)
}

// // 更新购物车纪录
func UpdateCart(c *gin.Context) {
	var code int
	var data model.Cart
	c.ShouldBindJSON(&data)
	code = model.UpdateCart(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}
